package com.settlement.system.service.impl;

import com.settlement.system.common.enums.FieldNameEnum;
import com.settlement.system.common.enums.ReportValueEnum;
import com.settlement.system.common.enums.SendReturnConditionEnum;
import com.settlement.system.common.util.common.JsonUtil;
import com.settlement.system.common.util.common.StrUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblWpSendReturnSqlConfigMapper;
import com.settlement.system.model.entity.common.TblWpSendReturnSqlConfig;
import com.settlement.system.model.vo.ConditionVo;
import com.settlement.system.service.ITblWpSendReturnSqlConfigService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 发退货字段配置表 服务实现类
 * @author hc
 * @since 2022-07-26
 */
@Service
public class TblWpSendReturnSqlConfigServiceImpl extends BaseServiceImpl implements ITblWpSendReturnSqlConfigService {

    @Resource
    private TblWpSendReturnSqlConfigMapper tblWpSendReturnSqlConfigMapper;

    @Override
    public BaseMapper init() {
        return tblWpSendReturnSqlConfigMapper;
    }

    @SneakyThrows
    @Override
    public void convertSendReturnSql(TblWpSendReturnSqlConfig model) {
        String replace = "";
        StringBuilder sqlBuilder = StrUtil.appendBuilder(new StringBuilder(" "));
        String fieldName = model.getFieldName();
        if(fieldName.startsWith("条件_")){
            StrUtil.appendBuilder(sqlBuilder," ( ");
            String conditionJson = model.getConditionJson();
            List<ConditionVo> conditionVos = JsonUtil.readListValue(conditionJson, ConditionVo.class);
            conditionVos.forEach(conditionVo->{
                if(StringUtils.isNotBlank(conditionVo.getKey())){
                    StrUtil.appendBuilder(sqlBuilder,
                            conditionVo.getKey()
                                    .replace("@和@","AND")
                                    .replace("@或@","OR")
                                    .replace("@和括号@","AND (")
                                    .replace("@或括号@","OR ("),
                            " ",SendReturnConditionEnum.witch(conditionVo.getConditionKey()).getSql()," ",conditionVo.getConditionValue()+" ");
                }else {
                    StrUtil.appendBuilder(sqlBuilder, SendReturnConditionEnum.witch(conditionVo.getConditionKey()).getSql()," ",conditionVo.getConditionValue()+" ");
                }
            });
            StrUtil.appendBuilder(sqlBuilder," ) ");
        }else{
            model.setReportValueSql(ReportValueEnum.witch(model.getReportValue()).getSql()+ (StringUtils.isNotEmpty(model.getReportValueAppend()) ? model.getReportValueAppend() : ""));
            StrUtil.appendBuilder(sqlBuilder,"CASE WHEN ");
            String conditionJson = model.getConditionJson();
            List<ConditionVo> conditionVos = JsonUtil.readListValue(conditionJson, ConditionVo.class);
            conditionVos.forEach(conditionVo->{
                if(StringUtils.isNotBlank(conditionVo.getKey())){
                    StrUtil.appendBuilder(sqlBuilder,
                            conditionVo.getKey()
                                    .replace("@和@","AND")
                                    .replace("@或@","OR")
                                    .replace("@和括号@","AND (")
                                    .replace("@或括号@","OR ("),
                            " ",SendReturnConditionEnum.witch(conditionVo.getConditionKey()).getSql()," ",conditionVo.getConditionValue()+" ");
                }else {
                    StrUtil.appendBuilder(sqlBuilder, SendReturnConditionEnum.witch(conditionVo.getConditionKey()).getSql()," ",conditionVo.getConditionValue()+" ");
                }
            });
            StrUtil.appendBuilder(sqlBuilder,"THEN ",model.getReportValueSql()," ELSE 0 END ");
        }
        replace = sqlBuilder.toString();
        replace=replace.replace("不包含","NOT IN")
                .replace("包含","IN")
                .replace("不类似","NOT LIKE")
                .replace("类似","LIKE")
                .replace("介于","BETWEEN")
                .replace(" 和 "," AND ")
                .replace(" 或 "," OR ");

        replace = convertSql(replace);

        model.setSqlStr(replace.trim());
    }

    public String convertSql(String sql){
        Pattern regex = Pattern.compile("--@\\w+",Pattern.CASE_INSENSITIVE);
        Matcher m = regex.matcher(sql);
        String replace = replace(regex, sql);
        m = regex.matcher(replace);
        if(m.find()){
            replace = replace(regex,replace);
        }
        return replace;
    }

    public String replace(Pattern regex, String script) {
        Matcher m = regex.matcher(script);
        StringBuffer sb = new StringBuffer();
        boolean finded = m.find();
        if (!finded) {
            return script;
        }
        while (finded) {
            String replaceKey = m.group(0);
            if(StringUtils.isNotBlank(replaceKey)){
                String key = FieldNameEnum.getCodeByReplaceKey(replaceKey.trim());
                if(StringUtils.isNotBlank(key)){
                    String sql = selectByFieldName(key);;
                    if(StringUtils.isNotBlank(sql)){
                        m.appendReplacement(sb, sql);
                    }
                }
            }
            finded = m.find();
        }
        m.appendTail(sb);
        String result = sb.toString() + System.getProperty("line.separator");
        return result;
    }

    @Override
    public String selectByFieldName(String fieldName) {
        return tblWpSendReturnSqlConfigMapper.selectByFieldName(fieldName);
    }
}

